#include <stdio.h>
#include "ex4.h"
#include "../../utils/utils.h"

#define MAX_LENGTH   1000    // the max length of one leng , if beyond this length will be ignore


int getLineTest() {

    /* print one line character, and print the length(split by \n). At the end ,print the longest line */

    int line[MAX_LENGTH];        // save every line character
    char longest[MAX_LENGTH];     // save the longest line

    int length;
    int maxLength;

    maxLength = 0;
    // get line
    while ((length = getLine(line, MAX_LENGTH)) != 0) {

        printf("length: %4d, %s", length, line);
        if (length > maxLength) {
            // copy line
            maxLength = length;
            copyLine(longest, line);
        }
    }

    printf("max length is %4d, %s\n", maxLength, longest);
    reverse(longest);
    printf("reverse result %s\n", longest);
    return 0;
}

void reverse(char s[]) {

    int c, lastIndex, tmp;
    lastIndex = 0;
    while ((c = s[lastIndex++]) != '\0') {
    }

    if (lastIndex <= 2) {
        return;
    }
    // 因为上面的循环多加了1，还要去掉最后一个 '\0'不处理
    lastIndex = lastIndex - 2;

    // 如果最后一个是 \n 也不用处理了，这里应该考虑 trim
    if (s[lastIndex] == '\n') {
        lastIndex--;
    }

    printf("%c,%c,%c\n", s[lastIndex], s[lastIndex - 1], s[lastIndex - 2]);
    for (int i = 0; (lastIndex - i - i) > 1; i++) {
        tmp = s[i];
        s[i] = s[lastIndex - i];
        s[lastIndex - i] = tmp;
    }
}
